﻿using MediatR;
using RBAC.Common.Domains.RabbitProject;
using RBAC.Common.Dto;
using RBAC.Common.Dto.ProSorce;
using RBAC.Common.infrastructures.interfaces;
using RBAC.Enum;

namespace RBAC.api.Application.commond.culture
{
    public class SourceHtmlHander : IRequestHandler<SourceHtmlCommond, Rulst>
    {
        /// <summary>
        /// 养殖信息管理
        /// </summary>
        private readonly IbaseRpository<breedMess> __breedMess;
        /// <summary>
        /// 省份
        /// </summary>
        private readonly IbaseRpository<Province> _Province;
        /// <summary>
        /// 市级id
        /// </summary>
        private readonly IbaseRpository<City> _city;
        /// <summary>
        /// 县级
        /// </summary>
        private readonly IbaseRpository<county> _county;
        /// <summary>
        /// 商品
        /// </summary>
        private readonly IbaseRpository<Goods> _Goods;
        /// <summary>
        /// 生产加工信息
        /// </summary>
        private readonly IbaseRpository<ProcessMess> _ProcessMess;
        /// <summary>
        /// 商品基本信息表
        /// </summary>

        private readonly IbaseRpository<ProductionBasicMess> _ProductionBasicMess;
        /// <summary>
        /// 图片合集
        /// </summary>
        private readonly IbaseRpository<ProductionPutImages> _ProductionPutImages;
        /// <summary>
        /// 屠宰信息
        /// </summary>
        private readonly IbaseRpository<slaughterMess> _SlaughtrMess;
        /// <summary>
        /// 保健信息
        /// </summary>
        private readonly IbaseRpository<VaccineMess> _VaccineMess;

        public SourceHtmlHander(IbaseRpository<breedMess> breedMess, IbaseRpository<Province> province, IbaseRpository<City> city, IbaseRpository<county> county, IbaseRpository<Goods> goods, IbaseRpository<ProcessMess> processMess, IbaseRpository<ProductionBasicMess> productionBasicMess, IbaseRpository<ProductionPutImages> productionPutImages, IbaseRpository<slaughterMess> slaughtrMess, IbaseRpository<VaccineMess> vaccineMess)
        {
            __breedMess = breedMess;
            _Province = province;
            _city = city;
            _county = county;
            _Goods = goods;
            _ProcessMess = processMess;
            _ProductionBasicMess = productionBasicMess;
            _ProductionPutImages = productionPutImages;
            _SlaughtrMess = slaughtrMess;
            _VaccineMess = vaccineMess;
        }

        public async Task<Rulst> Handle(SourceHtmlCommond request, CancellationToken cancellationToken)
        {

            var query = new Rulst() { Code=(int) Reult.success};
            var paging=new SourceHtmlDto() { count=0,Page=0};

            var result = (from a in _Goods.Query()
                         join b in _ProductionBasicMess.Query() on a.Goodsid equals b.Goodsid
                         select new SourceDto()
                         {

                             expirationtime=b.expirationtime,
                             FoundName="张三",
                             FoundTime="2024-05-03",
                             GETTheyperfect="",
                             GoodName=a.GoodsName,
                             ProductionBath=b.ProductionBath,
                             ProDuctionenterprise=b.ProDuctionenterprise,
                             ProDuctionid=b.ProDuctionBasicid,
                             ProDuctionNum=b.ProDuctionNum
                         }).ToList();



            



            paging.count=result.Count;
            paging.Page = (int)Math.Ceiling(result.Count*1.0 / request.pagesize);


            result=result.OrderBy(x=>x.ProDuctionid).Skip(request.pageindex-1).Take(request.pagesize).ToList();

            paging.DATA = result;

            ///首先判断我们的基本信息两条（）
            ///分别是图片+基本信息填写
            var Putresult = result;

            foreach (var item in Putresult) {


                var c = 0;
                //必须赋值为0
                c = 0;
                var list = _ProductionBasicMess.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid).FirstOrDefault();

                if (list.ProDuctionenterprise != "" && list.ProDuctionNum != null && list.expirationtime != null && list.expirationtime != null &&list.Goodsid!=0)
                {
                    c++;
                }
                if (list.ProDuctionImages!=null)
                   
                {
                    c++;
                }

                ///养殖信息和企业信息
                ///通过我们的foreach循环获得我们的序列号id
                ///通过查询 到的数据 去判断 是否有数据
                var listone = __breedMess.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid).FirstOrDefault();
                if(listone!=null)
                {
                    if (listone.varietyName != null && listone.Breedcome != null && listone.BreedDay > 0 && listone.Fodderbrand != null && listone.FodderName != null && listone.ColumnTime.ToString() != null)
                    {
                        c++;
                    }
                    if (listone.Farm != null && listone.FarmName != null && listone.Framnum > 0)
                    {
                        c++;
                    }
                }
                
                ///这里我的图片单独给了一张表
                ///他们的判断通过枚举可用 可不用
                ///获取我们具体的那个部分的图片 
                var listones = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid && x.Stypeid== Typenum.企业照片).ToList();
                if (listones != null)
                {

                    c++;

                    var listonet = _VaccineMess.Query().Where(x => x.VaccioneMessid == item.ProDuctionid).FirstOrDefault();
                    if (listonet != null)
                    {
                        if (listonet.RearDay > 0 && listonet.HealthcareMethod != null && listonet.HealThcareMode != null)
                        {
                            c++;
                        }

                    }
                   
                }



                var listttwo = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid && x.Stypeid == Typenum.动检证).ToList();
               
                if(listttwo!=null)
                {
                    c++;
                }

                ////屠宰信息的查询 和 养殖信息的思路一样

                var listtwo = _SlaughtrMess.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid).FirstOrDefault();
                if(listtwo!=null)
                {
                    if (listtwo.Slaughterlot != null && listtwo.SlaugherWeight > 0 && listtwo.SlaugherComeTime != null && listtwo.SlaugherTime != null)
                    {
                        c++;
                    }
                    if (listtwo.BossName != null && listtwo.Provinceid > 0 && listtwo.Cityid > 0 && listtwo.countyid > 0 && listtwo.SetupTime != null && listtwo.BossIntroMess != null)
                    {
                        c++;
                    }
                    ///因为我们上面查询到了专属的图片表
                    ///只需要给他拿过来直接用就行
                    ///
                    var three = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid && x.Stypeid == Typenum.相关资质).ToList();


                    if (three != null)
                    {
                        c++;
                    }

                    var four = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid && x.Stypeid == Typenum.相关资质).ToList();
                    if (four != null)
                    {
                        c++;
                    }
                }
                
                ////这里是我们的深加工的一个判断

                var listthree = _ProcessMess.Query().Where(x => x.Processid == item.ProDuctionid).FirstOrDefault();

                if (listthree != null) {

                    if (listthree.ProcessBossName != null && listthree.Processlicence != null)
                    {
                        c++;
                    }
                    if (listthree.ProcessLot != null)
                    {
                        c++;
                    }
                    if (listthree.Processclass != null && listthree.ProcessPersonincharge != null)
                    {
                        c++;
                    }


                    var five = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid && x.Stypeid == Typenum.食品安全).ToList();
                    if (five != null)
                    {
                        c++;
                    }
                    var six = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid && x.Stypeid == Typenum.食品安全照片).ToList();

                    if (six != null)
                    {
                        c++;
                    }
                    var server = _ProductionPutImages.Query().Where(x => x.ProDuctionBasicid == item.ProDuctionid && x.Stypeid == Typenum.加工企业).ToList();
                    if (server!=null)
                    {
                        c++;
                    }

                }
                





                var percent = (int)Math.Ceiling((c*1.0 / 17)*100) + "%";

                item.GETTheyperfect= percent;

            }

            query.data = paging;


            return query;



        }
    }
}
